import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: {
      name: "login"
    }
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/home'),
    children:[
      {
        path: '/user',
        name: 'user',
        component: () => import('../views/user')
      },
      {
        path: '/role',
        name: 'role',
        component: ()=>import('../views/role')
      }
    ]
  },
  {
    path: '/about',
    name: 'about',
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path:'/login',
    name: 'login',
    component: ()=>import('../views/login')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
  // scrollBehavior 用于定义路由切换时，页面滚动。
  scrollBehavior: () => ({
    y: 0
  })
})
// 解决相同路径跳转报错
const routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return routerPush.call(this, location, onResolve, onReject)
  return routerPush.call(this, location).catch(error => error)
};
export default router
